import java.util.Arrays;

public class Test {


    public static void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 12};
        moveZeroes(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
    }

    public static void moveZeroes(int[] nums) {
        int desc = -1;
        for (int cur = 0; cur < nums.length; cur++) {
            if (nums[cur] != 0) {
                desc++;
                int tmp = nums[cur];
                nums[cur] = nums[desc];
                nums[desc] = tmp;
            }
        }
    }

    public void duplicateZeros1(int[] arr1) {
        int[] arr2 = new int[arr1.length];
        int dest = 0;
        int cur = 0;
        while (dest < arr2.length) {
            if (arr1[cur] != 0) {
                arr2[dest++] = arr1[cur++];
            } else {
                if (dest < arr2.length) {
                    arr2[dest++] = 0;
                }
                if (dest < arr2.length) {
                    arr2[dest++] = 0;
                }
                cur++;
            }
        }

        for (int i = 0; i < arr2.length; i++) {
            arr1[i] = arr2[i];
        }
    }


    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = 0;
        int size = arr.length;

        while (dest < size) {
            if (arr[cur] != 0) {
                dest++;
            } else {
                dest += 2;
            }
            if (dest - 1 >= size) {
                break;
            }
            cur++;
        }

        if (dest == size) {
            arr[size] = 0;
            dest -= 2;
            cur--;
        }
        while (cur >= 0) {
            if (arr[cur] != 0) {
                arr[dest] = arr[cur];
            } else {
                arr[dest--] = 0;
                arr[dest--] = 0;
            }
        }
    }


    public boolean isHappy1(int n) {
        int slow = n;
        int fast = n;
        while (true) {
            slow = square(slow);
            fast = square(fast);
            fast = square(fast);

            if (slow == fast && slow == 1) return true;
            if (slow == fast && slow != 1) return false;
        }
    }

    public boolean isHappy2(int n) {
        int slow = n;
        int fast = square(n);
        while (slow != fast) {
            slow = square(slow);
            fast = square(square(fast));
        }
        return slow == 1;
    }

    public int square(int num) {
        int digit = 0;
        while (num > 0) {
            int remainder = num % 10;
            digit += remainder * remainder;
            num /= 10;
        }
        return digit;
    }

    public int maxArea1(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int ret = 0;
        while (left < right) {
            int v = Math.min(height[left], height[right]) * right - left;
            ret = Math.max(ret, v);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return ret;
    }


    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int ret = 0;
        while (left < right) {
            int v = Math.min(height[left], height[right]) * (right - left);
            ret = Math.max(ret, v);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return ret;
    }

    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int count = 0;
        for (int max = nums.length - 1; max >= 2; max--) {
            int left = 0;
            int right = max - 1;
            while (left < right) {
                if (nums[left] + nums[right] > nums[max]) {
                    count++;
                    right--;
                } else {
                    left--;
                }
            }
        }
        return count;
    }

    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0, len = Integer.MAX_VALUE;
        for (int left = 0, right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                len = Math.min(len, right - left + 1);
                sum -= nums[left++];
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;

    }


}
